﻿using System;
using DDDNorthWind.AC.Products.Domain.DomainEvents;
using DDDNorthWind.AC.Products.Domain.Entities;
using DDDNorthWind.AC.Products.Domain.Repository;
using DDDNorthWind.AC.Products.Domain.ValueObject;
using DDDNorthWind.Core.Domain.DomainEvents;
using DDDNorthWind.Core.Domain.EventArgs;
using DDDNorthWind.Core.Domain.Events;
using DDDNorthWind.Core.Domain.Services;

namespace DDDNorthWind.AC.Products.Domain.Services
{
    public class ProductServices : IProductServices
    {
        private readonly IProductRepository productRepository;

        public ProductServices(IProductRepository productRepository)
        {
            this.productRepository = productRepository;
        }


        public event InterDomainEventHandler<ProductSizedChanged> ProductSizeChanged;

        public void InvokeProductSizeChanged(EventAppliedEventArgs<ProductSizedChanged> e)
        {
            var handler = ProductSizeChanged;
            if (handler != null) 
                handler(this, e);
        }

        public void CreateNewProduct(Guid token)
        {
            var productId = new ProductId(token);

            var canAdd = !Exist(productId);
            if (canAdd)
                AddProduct(productId);
        }

        private void AddProduct(ProductId id)
        {
            var product = new Product(id);
            productRepository.Add(product);
        }

        public void AssignImportSizes(ProductId productId, double high, double length, double width)
        {
            var product = productRepository.ById(productId);
            product.AssignImportSizes(high,length,width);

            
            
            

        }

        private  bool Exist(ProductId productId)
        {
            var result = productRepository.ById(productId);
            return result != null;
        }
        public void Dispose()
        {
            productRepository.Dispose();
        }
    }
}